package org.travel.api.travel.controller.Chao;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.travel.api.travel.dao.Chao.AirportDao;
import org.travel.api.travel.dao.Chao.AreaDao;
import org.travel.api.travel.dao.Chao.FlightsDao;
import org.travel.api.travel.pojo.Airport;
import org.travel.api.travel.pojo.Area;
import org.travel.api.travel.pojo.Flights;
import org.travel.api.travel.utils.MySpecification;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/Chao")
@CrossOrigin("*")
public class ChaoController {

    @Autowired
    private FlightsDao flightsDao;

    @Autowired
    private AreaDao areaDao;

    @Autowired
    private AirportDao airportDao;

    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> GetAll(Flights condition, @RequestParam(defaultValue="1")int page, @RequestParam(defaultValue="3")int size) throws  Exception{

        if(condition.getTakeoffAirportId()!=null){
           Airport air = airportDao.getByCityid(condition.getTakeoffAirportId());
           condition.setTakeoffAirportId(air.getAirportId());
           Airport air1 = airportDao.getByCityid(condition.getLandingAirportId());
           condition.setLandingAirportId(air1.getAirportId());
       }


       Map<String,Object> map = new HashMap<>();
       Page<Flights> list = flightsDao.findAll(new MySpecification<>().and(
                MySpecification.Cnd.eq("takeoffAirportId",condition.getTakeoffAirportId()),
                MySpecification.Cnd.eq("landingAirportId",condition.getLandingAirportId())
        ), PageRequest.of(page-1,size));
       map.put("list",list.getContent());
       map.put("totle",list.getTotalElements());


        return map;
    }

    @RequestMapping("/ById")
    @ResponseBody
    public Flights GetId()throws  Exception{


        return null;
    }

}
